package com.unicom.priceboard.data.hastc;

import java.io.InputStream;
import java.util.HashMap;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import com.unicom.priceboard.data.BaseXMLParser;
import com.unicom.priceboard.data.vo.BestPriceInfo;

/**
 * @author quan
 *
 */
public class BestPriceParser extends BaseXMLParser {
	
	static HashMap<Integer, BestPriceInfo> lstBestPrice = new HashMap<Integer,BestPriceInfo>();
	private BestPriceInfo tempInfo;
	private StringBuffer tempVal;
	
	@Override
	public void parseDocument(InputStream xmlFile) {
		try{
			super.parseDocument(xmlFile);
		}catch(Exception ex)
		{
		}
	}
	
	@Override
	public void characters(char[] ch, int start, int length) throws SAXException {
//		tempVal = new String(ch,start,length);
		tempVal.append(ch,start,length);
	}

	@Override
	public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
		tempVal = new StringBuffer("");
		if(qName.equalsIgnoreCase("STS_Top3A"))
			tempInfo = new BestPriceInfo();
	}

	@Override
	public void endElement(String uri, String localName, String qName) throws SAXException {
		if(qName.equalsIgnoreCase("STOCK_ID")){
			tempInfo.setSTOCK_ID(Integer.parseInt(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("TRADING_DATE")){
			tempInfo.setTRADING_DATE(tempVal.toString());
		}else if(qName.equalsIgnoreCase("B_ORD_QTTY_3")){
			tempInfo.setBestBuy3Vol(Integer.parseInt(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("B_ORD_PRICE_3")){
			tempInfo.setBestBuy3Price(Long.parseLong(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("B_ORD_QTTY_2")){
			tempInfo.setBestBuy2Vol(Integer.parseInt(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("B_ORD_PRICE_2")){
			tempInfo.setBestBuy2Price(Long.parseLong(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("B_ORD_QTTY_1")){
			tempInfo.setBestBuy1Vol(Integer.parseInt(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("B_ORD_PRICE_1")){
			tempInfo.setBestBuy1Price(Long.parseLong(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("S_ORD_PRICE_1")){
			tempInfo.setBestOffer1Price(Long.parseLong(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("S_ORD_QTTY_1")){
			tempInfo.setBestOffer1Vol(Integer.parseInt(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("S_ORD_PRICE_2")){
			tempInfo.setBestOffer2Price(Long.parseLong(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("S_ORD_QTTY_2")){
			tempInfo.setBestOffer2Vol(Integer.parseInt(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("S_ORD_PRICE_3")){
			tempInfo.setBestOffer3Price(Long.parseLong(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("S_ORD_QTTY_3")){
			tempInfo.setBestOffer3Vol(Integer.parseInt(tempVal.toString()));
		}else if(qName.equalsIgnoreCase("STS_Top3A")){
			lstBestPrice.put(tempInfo.getSTOCK_ID(), tempInfo);
		}
			
	}

	/**
	 * @return the lstBestPrice
	 */
	public static HashMap<Integer, BestPriceInfo> getLstBestPrice() {
		return lstBestPrice;
	}
	public static void resetBestPrice(){
		lstBestPrice.clear();
	}
	/**
	 * @param lstBestPrice the lstBestPrice to set
	 */
	public static void setLstBestPrice(HashMap<Integer, BestPriceInfo> lstBestPrice) {
		BestPriceParser.lstBestPrice = lstBestPrice;
	}

	
}
